

var sites = require('../utils/sites')
var cheerio = require('cheerio'); 

class dm5 {

    constructor() {
        this.baseUrl = 'http://www.dm5.com'
    }
    /**
     * 获取漫画分类
     */
    async getComicCategory(){
        let categoryList = []                                
        await sites.asyncHttp(this.baseUrl + '/manhua-list/').then(function(res){
            let $ = cheerio.load(res)
            let new_span_bakList = $('.new_span_bak'); 
            let titlArray = new_span_bakList[0].children; 
            for (var i = 0; i < titlArray.length; i ++ ) {
                let titleObj = titlArray[i]
                if (titleObj.name == 'a') {
                    categoryList.push( {
                        name:titleObj.children[0].data, 
                        categoryUrl:titleObj.attribs.href
                    })
                }
            }
        })
        return categoryList
    }

    /**
     * 获取漫画分类列表页面
     * @param {*} categoryUrl 
     */
    async getComicPageList(categoryUrl){
        let comicPageList = []
        await sites.asyncHttp(this.baseUrl+categoryUrl).then(function(res){
            let $ = cheerio.load(res);
            let redzi = $('.redzi');
            let totalPageStr = redzi[1].children[0].data;
			let splitIndex = totalPageStr.indexOf("/");
            let totalPage = totalPageStr.slice(splitIndex+1,totalPageStr.length);
            for(let i = 0;i<totalPage;i++){
                comicPageList.push({
                    index:i,
                    pageUrl:categoryUrl.slice(0,-1)+'-p'+i+'/'
                })
            }
        })
        return comicPageList
    }

    /**
     * 获取漫画列表
     * @param {*} pageUrl 
     */
    async getComicList(pageUrl){
        let comicList = []
        await sites.asyncHttp(this.baseUrl+pageUrl).then(function(res){
            let $ = cheerio.load(res)
            let red_ljList = $('.red_lj');
            for(let i= 0;i<red_ljList.length;i++){
                comicList.push({
                    name:sites.filterString(red_ljList[i].children[1].attribs.title),
                    url:red_ljList[i].children[1].attribs.href
                })
            }            
        })
        return comicList
    } 
    /** 
     * 获取漫画详情
     */
    async getComicSynopsis(comicUrl){
        let comic ={
            list:[]
        }
        await sites.asyncHttp(this.baseUrl+comicUrl).then(function(res){
            let $= cheerio.load(res)
            let comicA = $("#cbc_1 a")
            for(let i =0 ;i<comicA.length;i++){
                comic.list.push({
                    name:comicA[i].attribs.title,
                    comicUrl:comicA[i].attribs.href,
                    pageCont:comicA[i].attribs.data
                })
            }
        })
        return comic        
    }

    /**
     * 下载单章漫画
     */
    async downloadComicBychapter(chapterUrl){
        let imgUrl = await sites.getImageURL(this.baseUrl+chapterUrl,'dm5')
        console.info(imgUrl)
        
    }
}

module.exports = dm5